import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutterarticle/bean/article_bean.dart';
import 'package:flutterarticle/network/api.dart';
import 'package:flutterarticle/pages/home/home_page.dart';

class SplashPage extends StatefulWidget{
  SplashPage({Key key,this.title}):super(key:key);
  final String title;

  State<StatefulWidget> createState(){
    return _SplashPageState();
  }
}

class _SplashPageState extends State<SplashPage>{
  bool _disposed = false;

  void initState(){
    //闪屏 维持3秒，之后随机读取文章 将articleBen 传给toHome函数
    Future.delayed(const Duration(seconds: 3),() {
      Article.random().then((article){
        toHome(article);
      }).catchError((e){
        toHome(null);
      });
    });
    super.initState();
  }

  void toHome(ArticleBean article){
    if(_disposed) return;
    Navigator.pushReplacement( //路由到 homePage页面
        context, MaterialPageRoute(builder: (context)=>HomePage(article)));
  }

  Widget build(BuildContext context){
    return Builder(builder: (context){
      return Container(
        child: Image(image: AssetImage('res/images/splash.jpg'),fit: BoxFit.fill,), //闪屏
      );
    });
  }

  void dispose(){
    _disposed = true;
    super.dispose();
  }
}